package com.markerhub.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.markerhub.common.dto.CourseUpdateDto;
import com.markerhub.common.lang.Result;
import com.markerhub.entity.*;
import com.markerhub.service.*;
import com.markerhub.util.ShiroUtil;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    CourseService courseService;
@Autowired
    ManagerActivityService activityService;
@Autowired
    ManagerTeacherService teacherService;
@Autowired
    ManagerClassService classService;






    @RequiresAuthentication
    @GetMapping("/course/{id}")
    public Result detail(@PathVariable(name = "id") Long id) {
       Course course= courseService.getById(id);
        Assert.notNull(course, "该课程已被删除");

        return Result.succ(course);
    }


    @GetMapping("/courses")
    public Result getCourseList(@RequestParam(defaultValue = "1") Integer currentPage,
                                @RequestParam Long teacherId) {
        Page<Course> page = new Page<>(currentPage, 5);

        // 只查询当前教师的课程
        IPage<Course> pageData = courseService.page(page, new LambdaQueryWrapper<Course>()
                .eq(Course::getTeacherId, teacherId)
                .orderByDesc(Course::getCreatedTime));

        return Result.succ(pageData);
    }


    @RequiresAuthentication
    @PostMapping("/update")
    public Result saveOrUpdateCourse(@RequestBody CourseUpdateDto courseUpdateDto) {

        ManagerActivity activity = activityService.getByActivityName(courseUpdateDto.getActivityName());
        if (activity == null) {
            return Result.fail("活动名称无效，请检查活动名称是否正确");
        }else {
            activityService.saveOrUpdate(activity);
        }
        ManagerTeacher teacher = teacherService.getByTeachername(courseUpdateDto.getTeacherName());
        if (teacher == null) {

            return Result.fail("教师名称无效，请检查教师名称是否正确");
        }else {
            teacherService.saveOrUpdate(teacher);
        }
        ManagerClass class1 = classService.getByClassName(courseUpdateDto.getClassName());
        if (class1 == null) {

            return Result.fail("教学班名称无效，请检查教学班名称是否正确");
        }else {
            classService.saveOrUpdate(class1);
        }
        Course course = new Course();
        if (courseUpdateDto.getId() != null) {
            course.setId(Math.toIntExact(courseUpdateDto.getId()));  // 设置课程ID用于更新
        }
        course.setCourseName(courseUpdateDto.getCourseName());
        course.setStatus(courseUpdateDto.getStatus());
        course.setTeacherId(teacher.getId());
        course.setClassId(class1.getId());
        course.setActivityId(Math.toIntExact(activity.getId()));
        courseService.saveOrUpdate(course);
        return Result.succ("课程保存成功");
    }
    @RequiresAuthentication
    @DeleteMapping("/delete/{id}")
    public Result deleteCourse(@PathVariable Long id) {
        boolean isDeleted = courseService.removeById(id);
        if (isDeleted) {
            return Result.succ("课程删除成功");
        } else {
            return Result.fail("课程删除失败，课程不存在");
        }
    }

    // 批量删除课程
    @RequiresAuthentication
    @PostMapping("/batch-delete")
    public Result batchDeleteCourses(@RequestBody Map<String, List<Long>> ids) {
        List<Long> courseIds = ids.get("ids");
        if (courseService.removeByIds(courseIds)) {
            return Result.succ("批量删除成功");
        } else {
            return Result.fail("批量删除失败");
        }
    }

    @RequiresAuthentication
    @GetMapping("/select")
    public Result getCourses(
            @RequestParam(value = "activityName", required = false) String activityName,
            @RequestParam(value = "courseName", required = false) String courseName,
            @RequestParam(value = "status", required = false) String status,
            @RequestParam(value = "teacherName", required = false) String teacherName,
            @RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
            @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {

        IPage<Course> courses = courseService.getCourses(activityName, courseName, status, teacherName, currentPage, pageSize);
        return Result.succ(courses);
    }


}
